package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.JsClassificationMapper;
import com.xinghai.system.domain.JsClassification;
import com.xinghai.system.service.IJsClassificationService;

/**
 * 分类Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-31
 */
@Service
public class JsClassificationServiceImpl implements IJsClassificationService 
{
    @Autowired
    private JsClassificationMapper jsClassificationMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询分类
     * 
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public JsClassification getInfo(Long id)
    {
        return jsClassificationMapper.getInfo(id);
    }

    /**
     * 查询分类列表
     * 
     * @param jsClassification 分类
     * @return 分类
     */
    @Override
    public List<JsClassification> list(JsClassification jsClassification)
    {
            jsClassification.setDelFlag("0");
        return jsClassificationMapper.list(jsClassification);
    }

    /**
     * 新增分类
     * 
     * @param jsClassification 分类
     * @return 结果
     */
    @Override
    public int insert(JsClassification jsClassification)
    {
        jsClassification.setCreateTime(DateUtils.getNowDate());
        return jsClassificationMapper.insert(jsClassification);
    }
    /**
     * 批量新增分类
     *
     * @param jsClassificationList 分类
     * @return 结果
     */
    @Override
    public int insertBatch(List<JsClassification> jsClassificationList)
    {
        return jsClassificationMapper.insertBatch(jsClassificationList);
    }
    /**
     * 修改分类
     * 
     * @param jsClassification 分类
     * @return 结果
     */
    @Override
    public int update(JsClassification jsClassification)
    {
        jsClassification.setUpdateTime(DateUtils.getNowDate());
        return jsClassificationMapper.update(jsClassification);
    }

    /**
     * 批量删除分类
     * 
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return jsClassificationMapper.deleteBatch(ids);
    }

    /**
     * 删除分类信息
     * 
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return jsClassificationMapper.delete(id);
    }

    /**
     * 导入分类信息
     *
     * @param list 分类信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JsClassification> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JsClassificationMapper jsClassificationMapper = sqlSession.getMapper(JsClassificationMapper.class);
            list.stream().forEach(item -> jsClassificationMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
